import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.events.TreeListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class SWTTest
{

    private Shell sShell = null;

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub

        /* Before this is run, be sure to set up the launch configuration (Arguments->VM Arguments)
         * for the correct SWT library path in order to run with the SWT dlls. 
         * The dlls are located in the SWT plugin jar.  
         * For example, on Windows the Eclipse SWT 3.1 plugin jar is:
         *       installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar
         */
        Display display = Display.getDefault();
        SWTTest thisClass = new SWTTest();
        thisClass.createSShell();
        thisClass.sShell.open();
        while (!thisClass.sShell.isDisposed())
        {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }

    /**
     * This method initializes sShell
     */
    private void createSShell()
    {
        sShell = new Shell();
        sShell.setText("Shell");
        sShell.setSize(new Point(300, 200));
        sShell.setLayout(new FillLayout());

        final Tree tree = new Tree(sShell, SWT.SINGLE);
        for (int i = 1; i < 4; i++)
        {
            TreeItem grandParent = new TreeItem(tree, 0);
            grandParent.setText("Grand Parent - " + i);
            for (int j = 1; j < 4; j++)
            {
                TreeItem parent = new TreeItem(grandParent, 0);
                parent.setText("Parent - " + j);
                for (int k = 1; k < 4; k++)
                {
                    TreeItem child = new TreeItem(parent, 0);
                    child.setText("Child - " + k);
                }
            }
        }
        tree.addSelectionListener(new SelectionAdapter()
        {
            public void widgetSelected(SelectionEvent event)
            {
                TreeItem[] selected = tree.getSelection();
                if (selected.length > 0)
                {
                    System.out.println("Selected: " + selected[0].getText());
                }
            }
        });

        tree.addTreeListener(new TreeListener()
        {

            public void treeCollapsed(TreeEvent e)
            {
                System.out.println("collapse");
            }

            public void treeExpanded(TreeEvent e)
            {
                System.out.println("expanded");

            }
        });

    }

}
